package com.js.common.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfiguration {

    /**
     * 事件缓存对象
     * @return
     */
    @Bean
    public CaffeineCache eventCache(){
        return new CaffeineCache("event", Caffeine.newBuilder().recordStats()
                .expireAfterWrite(1, TimeUnit.MINUTES)
                .maximumSize(200)
                .build());
    }

    /**
     * 天缓存对象
     * @return
     */
    @Bean
    public CaffeineCache dayCache(){
        return new CaffeineCache("day", Caffeine.newBuilder().recordStats()
                .expireAfterWrite(1, TimeUnit.DAYS)
                .maximumSize(200)
                .build());
    }

    /**
     * 月缓存对象
     * @return
     */
    @Bean
    public CaffeineCache monthCache(){
        return new CaffeineCache("month", Caffeine.newBuilder().recordStats()
                .expireAfterWrite(30, TimeUnit.DAYS)
                .maximumSize(200)
                .build());
    }
}